﻿using H2.NET.Common;
using H2.NET.Web.Workflow.DwzCode;
using H2.NET.Web.Workflow.Models;
using H2.NET.Web.Workflow.Services;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace H2.NET.Web.Workflow.Workflow
{
    public partial class ExecutorMaintain : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var jobService = new JobService();
            var groupService = new GroupService();
            var action = AksRequest.GetQueryString("action");

            switch (action)
            {
                case "searchName":
                    {
                        var keyword = AksRequest.GetString("keyword");

                        var result = jobService.FindByKey(keyword).Select(t =>
                            new
                            {
                                id = t.name.Trim(),
                                name = t.name.Trim()
                            });

                        Response.ContentType = "text/json";
                        Response.Write(JsonConvert.SerializeObject(result));
                        Response.End();
                        return;
                    }
                case "save":
                    {
                        var name = AksRequest.GetString("name");
                        var fullname = AksRequest.GetString("fullname");
                        if (jobService.UpdateFullName(name, fullname))
                        {
                            SuccessMsg();
                        }
                        else
                        {
                            ErrorMsg("更新失败");
                        }
                        return;
                    }
                case "searchFullName":
                    {
                        var name = AksRequest.GetString("name");
                        var jobs = jobService.GetByName(name);
                        Response.Write(jobs.fullname.Trim());
                        Response.End();
                        return;
                    }
                case "searchGroupName":
                    {
                        var keyword = AksRequest.GetString("keyword");
                        var result = groupService.FindGroupNamesByKey(keyword).Select(t =>
                            new
                            {
                                id = t.Trim(),
                                name = t.Trim()
                            });
                        Response.ContentType = "text/json";
                        Response.Write(JsonConvert.SerializeObject(result));
                        Response.End();
                        return;
                    }
                case "searchGroups":
                    {
                        var gname = AksRequest.GetString("gname");
                        var result = groupService.FindGroupsByGname(gname).Select(t =>
                            new
                            {
                                gname = t.Gname.Trim(),
                                member = t.Member.Trim(),
                                membertype = t.MemberType,
                                grouptype = t.GroupType
                            });

                        Response.ContentType = "text/json";
                        Response.Write(JsonConvert.SerializeObject(result));
                        Response.End();
                        return;
                    }
                case "savegroup":
                    {
                        var actiontype = AksRequest.GetString("actiontype");

                        var groups = new Groups();
                        groups.Gname = AksRequest.GetString("gname");
                        groups.OldMember = AksRequest.GetString("e_oldmember");
                        groups.Member = AksRequest.GetString("e_member");
                        groups.MemberType = AksRequest.GetInt("e_membertype", 0);
                        groups.GroupType = AksRequest.GetInt("e_grouptype", 0);

                        groupService.Maintain(groups, actiontype);
                        SuccessMsg();
                        return;
                    }
            }
        }
    }
}